import defaultSettings from '../settings.json';
import { AuthorityResponse } from '@/constants/type';

export interface GlobalState {
  settings?: typeof defaultSettings;
  userInfo?: {
    username?: string;
    avatar?: string;
    location?: string;
    email?: string;
    admin?: boolean;
    permissions: Record<string, string[]>;
    authorities: AuthorityResponse[];
  };
  userLoading?: boolean;
}

const initialState: GlobalState = {
  settings: defaultSettings,
  userInfo: {
    permissions: {},
    authorities: []
  }
};

export default function store(state = initialState, action) {
  switch (action.type) {
    case 'update-settings': {
      const { settings } = action.payload;
      return {
        ...state,
        settings
      };
    }
    case 'update-userInfo': {
      const { userInfo = initialState.userInfo, userLoading } = action.payload;
      return {
        ...state,
        userLoading,
        userInfo
      };
    }
    default:
      return state;
  }
}
